package com.ykyl.config;

import com.ykyl.intercept.UserInterceptor;
import com.ykyl.intercept.UserTokenInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@RequiredArgsConstructor
public class SpringMvcSupport implements WebMvcConfigurer {
    
    // 自动注入拦截器
    private final UserInterceptor userInterceptor;
    private final UserTokenInterceptor userTokenInterceptor;

    private static final String[] EXCLUDE_PATH_PATTERNS = new String[]{"/swagger-ui.html",
            "/webjars/**",
            "/swagger-resources",
            "/v2/api-docs",
            // 登录接口
            "/customer/user/login",
            // 房型列表接口
            "/customer/roomTypes",
            "/customer/orders/project/**",
            "/user/refresh/**"};

    //拦截的时候过滤掉swagger相关路径和登录相关接口
    private static final String[] ADMIN_EXCLUDE_PATH_PATTERNS = new String[]{"/swagger-ui.html",
            "/webjars/**",
            "/swagger-resources",
            "/v2/api-docs",
            "/customer/**",
            "/security/**",
            "/common/**",
            "/user/refresh/**"};

    /**
     * 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //后台的拦截器
        registry.addInterceptor(userTokenInterceptor).addPathPatterns("/**").excludePathPatterns(ADMIN_EXCLUDE_PATH_PATTERNS);
        // 小程序端接口鉴权拦截器
        registry.addInterceptor(userInterceptor).addPathPatterns("/customer/**").excludePathPatterns(EXCLUDE_PATH_PATTERNS);
    }

}